package cn.skyquant.quant4j.api.strategy;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class StrategyTestServiceProxy extends BaseServiceProxy implements StrategyTestService {

    private static final String url = "/strategy_test";

    public StrategyTestServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }

    @Override
    public ResultDTO<Integer> start(String strategyName,String version,String name,String userName,long beginDate,String paramstr,double initMoney) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("strategyName", strategyName);
        paramMap.put("version", version);
        paramMap.put("name", name);
        paramMap.put("beginDate", String.valueOf(beginDate));
        paramMap.put("paramstr", paramstr);
        paramMap.put("initMoney", String.valueOf(initMoney));
        JSONObject jsonObject = post(url + "/start", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success(jsonObject.getIntValue("data"));
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> updateDaily(int id, long thedate, double balance, double equity, double open, double close, double high, double low) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("thedate", String.valueOf(thedate));
        paramMap.put("equity", String.valueOf(equity));
        paramMap.put("balance", String.valueOf(balance));
        paramMap.put("open", String.valueOf(open));
        paramMap.put("close", String.valueOf(close));
        paramMap.put("high", String.valueOf(high));
        paramMap.put("low", String.valueOf(low));
        JSONObject jsonObject = post(url + "/"+id+"/updateDaily", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> end(int id, long thedate, double equity) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("thedate", String.valueOf(thedate));
        paramMap.put("equity", String.valueOf(equity));
        JSONObject jsonObject = post(url + "/"+id+"/end", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> delete(int id) {
        JSONObject jsonObject = post(url + "/"+id+"/delete", null);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> addOpen(int id, long thedate, String symbol, double openPrice, double amount, String action,Double sl, Double tp) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("thedate", String.valueOf(thedate));
        paramMap.put("symbol", symbol);
        paramMap.put("openPrice", String.valueOf(openPrice));
        paramMap.put("amount", String.valueOf(amount));
        paramMap.put("action", action);
        if(sl!=null){
            paramMap.put("sl", String.valueOf(sl));
        }
        if(tp!=null){
            paramMap.put("tp", String.valueOf(tp));
        }
        JSONObject jsonObject = post(url + "/"+id+"/addOpen", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> addClose(int id, long thedate, String symbol, double avgPrice,double closePrice, double amount,String action, double profit,Double commission) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("thedate", String.valueOf(thedate));
        paramMap.put("symbol", symbol);
        paramMap.put("closePrice", String.valueOf(closePrice));
        paramMap.put("avgPrice", String.valueOf(avgPrice));
        paramMap.put("amount", String.valueOf(amount));
        paramMap.put("profit", String.valueOf(profit));
        paramMap.put("action", action);
        if(commission!=null){
            paramMap.put("commission", String.valueOf(commission));
        }
        JSONObject jsonObject = post(url + "/"+id+"/addClose", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }

    @Override
    public ResultDTO<Void> addStatistics(int id, long beginTime, long endTime, double beginMoney, double endMoney, int tradeLevel,double profit, double amount,int distance) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("beginTime", String.valueOf(beginTime));
        paramMap.put("endTime", String.valueOf(endTime));
        paramMap.put("beginMoney", String.valueOf(beginMoney));
        paramMap.put("endMoney", String.valueOf(endMoney));
        paramMap.put("tradeLevel", String.valueOf(tradeLevel));
        paramMap.put("profit", String.valueOf(profit));
        paramMap.put("amount", String.valueOf(amount));
        paramMap.put("distance", String.valueOf(distance));
        JSONObject jsonObject = post(url + "/"+id+"/addStatistics", paramMap);
        if (jsonObject != null) {
            boolean ok = jsonObject.getBoolean("ok");
            if (ok) {
                return ResultDTO.success;
            }
        }
        return ResultDTO.error;
    }
}
